import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:xiaomi_shop/app/api/api.dart';
import 'package:xiaomi_shop/app/services/screenAdapter.dart';
import 'package:xiaomi_shop/models/index.dart';

import '../controllers/shop_controller.dart';

class ShopView extends GetView<ShopController> {
  const ShopView({Key? key}) : super(key: key);
  // 筛选
  Widget _selectView() {
    return Positioned(
      top: 0,
      left: 0,
      right: 0,
      child: SizedBox(
          width: ScreenAdapter.width(1080),
          height: ScreenAdapter.height(100),
          child: Obx(() => Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: controller.subHeaderList.map((value) {
                  return GestureDetector(
                    onTap: () {
                      controller.selectCondition(value);
                    },
                    child: Text(
                      value["title"],
                      style: TextStyle(
                          fontSize: ScreenAdapter.fontSize(38),
                          color:
                              controller.selectConditionId.value == value['id']
                                  ? Colors.red
                                  : Colors.black),
                    ),
                  );
                }).toList(),
              ))),
    );
  }

  // 商品列表
  Widget _shopList() {
    return Positioned(
        top: ScreenAdapter.height(100),
        left: 0,
        bottom: 0,
        right: 0,
        child: Obx(() => Container(
            color: const Color.fromARGB(255, 246, 246, 246),
            child: EasyRefresh(
              controller: controller.refreshController,
              onRefresh: () async {
                controller.onRefresh();
              },
              onLoad: () async {
                controller.onLoadMore();
              },
              child: ListView.builder(
                padding: EdgeInsets.all(ScreenAdapter.width(20)),
                itemCount: controller.shopList.length,
                itemBuilder: (context, index) {
                  ShopModel model = controller.shopList.value[index];
                  return InkWell(
                      onTap: () {
                        Get.toNamed('/shopDetail', arguments: {"id": model.id});
                      },
                      child: Container(
                        padding: EdgeInsets.all(ScreenAdapter.width(20)),
                        margin:
                            EdgeInsets.only(bottom: ScreenAdapter.width(20)),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius:
                              BorderRadius.circular(ScreenAdapter.width(16)),
                        ),
                        child: Row(
                          children: [
                            Container(
                              padding: EdgeInsets.fromLTRB(
                                  0,
                                  ScreenAdapter.width(20),
                                  ScreenAdapter.width(20),
                                  ScreenAdapter.width(20)),
                              width: ScreenAdapter.width(340),
                              child:
                                  Image.network("${Api.basicUrl}/${model.pic}"),
                            ),
                            Expanded(
                                child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  model.title!,
                                  style: TextStyle(
                                      fontSize: ScreenAdapter.fontSize(40),
                                      fontWeight: FontWeight.bold,
                                      overflow: TextOverflow.ellipsis),
                                  maxLines: 2,
                                ),
                                SizedBox(
                                  height: ScreenAdapter.height(10),
                                ),
                                Text(
                                  model.sub_title!,
                                  style: TextStyle(
                                      color: Colors.black45,
                                      fontSize: ScreenAdapter.fontSize(30),
                                      overflow: TextOverflow.ellipsis),
                                  maxLines: 2,
                                ),
                                SizedBox(
                                  height: ScreenAdapter.height(10),
                                ),
                                Text(
                                  '¥ ${model.price}',
                                  style: TextStyle(
                                      fontSize: ScreenAdapter.fontSize(40)),
                                ),
                              ],
                            ))
                          ],
                        ),
                      ));
                },
              ),
            ))));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: controller.globalKey,
      appBar: AppBar(
        title: const Text('商品列表'),
        centerTitle: true,
        scrolledUnderElevation: 0,
        actions: [Text("")],
      ),
      endDrawer: Drawer(),
      body: Stack(
        children: [_selectView(), _shopList()],
      ),
    );
  }
}
